/**
 * CLONED SKINNED MESH.
 */
package  
{
	import flare.basic.*;
	import flare.core.*;
	import flare.loaders.collada.*;
	import flare.system.*;
	import flare.utils.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	
	[SWF(frameRate = 120, backgroundColor = 0x808080, width = 800, height = 600 )]
	/**
	 * ...
	 * @author Ariel Nehamd
	 */
	public class Test07_Skin extends Sprite
	{
		private var scene:Scene3D;		
		private var model:Pivot3D;
		
		public function Test07_Skin() 
		{
			addChild( new Stats() );
			
			// NOTE: this is an intensive test, 
			// try to compile in "Release Mode". There is a lot off difference.
			
			ColladaLoader.texturesFolder = "textures";
			
			scene = new Viewer3D( this );
			scene.addEventListener( Scene3D.COMPLETE_EVENT, completeEvent );
			
			model = scene.addChildFromFile( "skin.f3d" );
			
			// light parameters.
			scene.ambientColor = new Vector3D( 0.2, 0.2, 0.2 );  // <-- RGB ambient light color.
			scene.defaultLight.color = new Vector3D( 1, 1, 1, 0.5 ); // <-- RGB light colors and contrast.
			scene.defaultLight.setRotation( 40, 40, 0 );			
		}
		
		private function completeEvent(e:Event):void 
		{
			var count:int = 10;
			for ( var h:int = 0; h < count; h++ )
			{
				for ( var v:int = 0; v < count; v++ )
				{
					var space:Number = 60;
					var cloned:Pivot3D = model.clone();
					cloned.x = h * space - space * count / 2;
					cloned.z = v * space - space * count / 2;
					// NOTE:, that if some models share the same frame, it is much faster.
					// you also will be able to group skinned models into layer to optimize the render.
					// this is the worst case, when all models had differents frames.
					cloned.frameSpeed = Math.random() + 0.5;
					cloned.parent = scene;
				}
			}
			
			// camera posiion and orientation.
			scene.camera.setPosition( 350, 150, -350 );
			scene.camera.lookAt( 0, 0, 0 );
			scene.camera.far = 10000;
			
			scene.play();			
			scene.removeChild( model );			
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
		}
		
		private function updateEvent(e:Event):void 
		{
			if ( Input3D.keyHit( Input3D.P ) ) scene.play()
			if ( Input3D.keyHit( Input3D.S ) ) scene.stop()
		}		
	}
}